using WeatherSystem.API.Services;

namespace WeatherSystem.API.Services
{
    /// <summary>
    /// Mock天气API服务实现 - 返回随机数据
    /// </summary>
    public class MockWeatherApiService : IWeatherApiService
    {
        private readonly ILogger<MockWeatherApiService> _logger;
        private readonly Random _random;
        
        // 天气状况列表
        private readonly string[] _weatherConditions = {
            "晴朗", "多云", "阴天", "小雨", "中雨", "大雨", 
            "雷阵雨", "雪", "雾", "霾", "沙尘暴"
        };

        public MockWeatherApiService(ILogger<MockWeatherApiService> logger)
        {
            _logger = logger;
            _random = new Random();
        }

        /// <summary>
        /// 获取模拟的天气数据
        /// </summary>
        public async Task<WeatherApiResponse> GetCurrentWeatherAsync(decimal latitude, decimal longitude)
        {
            // 模拟API调用延迟
            await Task.Delay(_random.Next(100, 500));
            
            // 生成随机天气数据
            var temperature = GenerateTemperature(latitude);
            var humidity = _random.Next(20, 95);
            var pressure = _random.Next(980, 1030) + _random.NextDouble();
            var windSpeed = _random.NextDouble() * 20; // 0-20 m/s
            var windDirection = _random.Next(0, 360);
            var visibility = _random.Next(1, 50); // 1-50 km
            var uvIndex = _random.Next(0, 12);
            var weatherCondition = _weatherConditions[_random.Next(_weatherConditions.Length)];

            var result = new WeatherApiResponse
            {
                Temperature = (decimal)temperature,
                Humidity = humidity,
                Pressure = (decimal)pressure,
                WindSpeed = (decimal)windSpeed,
                WindDirection = windDirection,
                Visibility = visibility,
                UVIndex = uvIndex,
                WeatherCondition = weatherCondition
            };

            _logger.LogInformation("Mock天气数据生成成功，位置: ({Lat}, {Lon}), 温度: {Temp}°C, 天气: {Weather}", 
                latitude, longitude, result.Temperature, result.WeatherCondition);

            return result;
        }

        /// <summary>
        /// 根据纬度生成合理的温度范围
        /// </summary>
        private double GenerateTemperature(decimal latitude)
        {
            var absLatitude = Math.Abs((double)latitude);
            
            // 根据纬度调整温度范围
            double baseTemp, range;
            
            if (absLatitude < 23.5) // 热带
            {
                baseTemp = 28;
                range = 15; // 20-35°C
            }
            else if (absLatitude < 40) // 亚热带
            {
                baseTemp = 20;
                range = 25; // 5-30°C
            }
            else if (absLatitude < 60) // 温带
            {
                baseTemp = 15;
                range = 30; // 0-25°C
            }
            else // 寒带
            {
                baseTemp = 0;
                range = 20; // -10-10°C
            }
            
            // 添加季节性变化（简化版）
            var seasonOffset = Math.Sin((DateTime.Now.DayOfYear / 365.0) * 2 * Math.PI) * 10;
            
            return baseTemp + seasonOffset + (_random.NextDouble() - 0.5) * range;
        }
    }
}